package fourfaith.com.ff_sl21_app.utils;

import android.content.Context;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.RetryPolicy;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;

import java.io.UnsupportedEncodingException;
import java.util.Map;

public class JsonRequest <T> extends Request<T> {
	private final Class<T> mClazz;
	private final Listener<T> mListener;
	private final Map<String, String> mHeaders;
	private Map<String,String> mParams;

	public JsonRequest(Integer method, String url, Class<T> clazz, Listener<T> listener, final Context context,Map<String,String> params) {
		this(method, url, clazz, null, listener,  new ErrorListener() {
			@Override
			public void onErrorResponse(VolleyError error) {
				if (error.networkResponse == null) {
					Toast.makeText(context, "连接超时，请求不到网络数据", Toast.LENGTH_SHORT).show();
				} else {
					if (error.getMessage() == null) {
						Toast.makeText(context, "接口服务器异常", Toast.LENGTH_SHORT).show();
					} else {
						Toast.makeText(context, error.getMessage(), Toast.LENGTH_SHORT).show();
					}
				}

			}
		},params);
	}

	public JsonRequest(Integer method, String url, Class<T> clazz, Listener<T> listener, ErrorListener errorListener) {
		this(method, url, clazz, null, listener, errorListener,null);
	}

	public JsonRequest(Integer method, String url, Class<T> clazz, Listener<T> listener, ErrorListener errorListener,Map<String,String> params) {
		this(method, url, clazz, null, listener, errorListener, params);
	}

	public JsonRequest(int method, String url, Class<T> clazz, Map<String, String> headers,
			Listener<T> listener, ErrorListener errorListener, Map<String,String> params) {
		super(method, url, errorListener);
		this.mClazz = clazz;
		this.mHeaders = headers;
		this.mListener = listener;
		//设置volley超时时间
		RetryPolicy retryPolicy = new DefaultRetryPolicy(30 * 1000, 0, 1.0f);
		this.setRetryPolicy(retryPolicy);
		this.mParams = params;
	}

	@Override
	public Request<?> setRetryPolicy(RetryPolicy retryPolicy) {
		//设置volley超时时间
		return super.setRetryPolicy(retryPolicy);
	}

	@Override
	public Map<String, String> getHeaders() throws AuthFailureError {
		return mHeaders != null ? mHeaders : super.getHeaders();
	}

	@Override
	protected void deliverResponse(T response) {
		mListener.onResponse(response);
	}

	@Override
	protected Response<T> parseNetworkResponse(NetworkResponse response) {
		try {
			String json = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
			return Response.success(JSON.parseObject(json, mClazz), HttpHeaderParser.parseCacheHeaders(response));
		} catch (UnsupportedEncodingException e) {
			return Response.error(new ParseError(e));
		}
	}

	//关键代码就在这里，在 Volley 的网络操作中，如果判断请求方式为 Post 则会通过此方法来获取 param，所以在这里返回我们需要的参数，
	@Override
	protected Map<String, String> getParams() throws AuthFailureError {
		return mParams;
	}
}
